package com.eagleyng.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamUtils {

	/**
	 * inputStream2String
	 * @param is
	 * @param encoding
	 * @return
	 */
	public static final String inputStream2String(InputStream is,String encoding){
		if(is == null){
			return null;
		}
		String rtn = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		int i = -1;
		try {
			while ((i = is.read()) != -1) {
				baos.write(i);
			}
			rtn = baos.toString(encoding);
			is.close();
			baos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return rtn;
	}
	
	/**
	 * inputStream2byte
	 * @param inStream
	 * @return
	 * @throws IOException
	 */
	public static final byte[] inputStream2byte(InputStream is)
			throws IOException {
		if(is == null){
			return null;
		}
		ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
		byte[] buff = new byte[100];
		int rc = 0;
		while ((rc = is.read(buff, 0, 100)) > 0) {
			swapStream.write(buff, 0, rc);
		}
		byte[] in2b = swapStream.toByteArray();
		return in2b;
	}
}
